#include <stdio.h>
#include "modbus.h"
#include "crc.h"

//#define TCP
void main(int argc, char *argv[])
{
    int i;
    int ret;
    uint8 crcHi, crcLo;
    uint16 outLen;

#ifndef TCP
    /*读多个*/
    //unsigned char buffer[256] = {0x03/*unitAddress*/, 0x03/*funcNo*/, 0x01, 0x01/*startAddress*/, 0x00, 0x7B, 0xD5, 0xCA };
    /*写单个*/
    //unsigned char buffer[256] = {0x03/*unitAddress*/, 0x06/*funcNo*/, 0x00, 0x01/*startAddress*/, 0x0A, 0x7B, 0xD5, 0xCA };
   /* 
    crcHi = CRC16( buffer, 6, 1 );
    crcLo = CRC16( buffer, 6, 0 );
    buffer[6] = crcHi;
    buffer[7] = crcLo;
*/
    /*写多个*/
    unsigned char buffer[256] = {0x03/*unitAddress*/, 0x10/*funcNo*/, 0x00, 0x01/*startAddress*/, 
                                 0x00, 0x02, 0x04, 0x0A, 0x0B, 0x0C, 0x0D };
    crcHi = CRC16( buffer, 11, 1 );
    crcLo = CRC16( buffer, 11, 0 );
    buffer[11] = crcHi;
    buffer[12] = crcLo;                             

#else
    uint8 buffer[256] = {0x19, 0xB2, 0x00, 0x00, 0x00, 0x06,
                         0x01/*unitAddress*/, 0x06/*funcNo*/, 0x01, 
                         0x00/*startAddress*/, 0xEF, 0x06 /*num or value*/, 
                         0x04 /*byte count*/,
                         0x0a,0x0b,0x0c,0x0d};
#endif

#ifndef TCP
    ModbusInit(MODBUS_RTU, 03);
#else
    ModbusInit(MODBUS_TCP, 01);
#endif   

    for (i = 0; i < MODBUS_REGISTER_NUM_MAX; i += 50)
    {
        if (-1 == ModbusAddRegisterGroup(i , i + 49 , 3, i + 1))
            printf("ModbusAddRegisterGroup error\r\n");
    }
    ModbusAddRegisterGroup(1, 10, 3, 0x17);

    ModbusWriteRegister(241, 0x99);
    ret = ModBusHandleRequest(buffer, &outLen);
    printf("ret:%d \r\n", ret);
    ModbusPrintRegisterList();
    PrintBuffer(buffer, 50);
     printf("outLen:%d \r\n", outLen);
    return;
}